﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Web.Configuration;

/// <summary>
/// Summary description for DALThanhVien
/// </summary>

public class DALThanhVien : StandardConnectDB, IThanhVien

{
    private SqlConnection sqlConn;
	public DALThanhVien()
	{
		//
		// TODO: Add constructor logic here
		//
        sqlConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["flowerseelingConnectionString"].ToString());
         
	}
    

    public bool CheckRegister(string email)
    {
        bool status;
        string sql = "select count(*) from ThanhVien where EMAIL= @Email";
        SqlCommand cmd = new SqlCommand(sql, sqlConn);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = email; 
        sqlConn.Open();
        if (Int32.Parse(cmd.ExecuteScalar().ToString()) > 0)
            status = false;
        else
            status = true;
        cmd.Dispose();
        sqlConn.Close();
        return status;
    }
 

    public bool CheckLogin(string email, string password)
    {
        bool status;
        string sql = "select count(*) from THANHVIEN where EMAIL= @Email and MATKHAU=@Password";
        SqlCommand cmd = new SqlCommand(sql, sqlConn);
        cmd.CommandType = CommandType.Text;
        cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = email;
        cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value = password;
        sqlConn.Open();
        if (Int32.Parse(cmd.ExecuteScalar().ToString()) > 0)
            status = true;
        else
            status = false;
        cmd.Dispose();
        sqlConn.Close();
        return status;
    }

    public string PasswordRecovery(string email)
    {
        string status;
        ThanhVien tv = new ThanhVien();
        tv = new DALThanhVien().SelectByEmail(email);
            if (tv!=null && tv.Email ==email)
            {
                Mail mail = new Mail();
                string body="Mật khẩu của tài khoản "+email+" là:  "+tv.MatKhau;
               status= mail.Send_Email(tv.Email, "Mật khẩu của bạn", body);
            }
            else
            {
                status = "Tài khoản này không tồn tại";  
            }
            return status;
      
    }

    public ThanhVien SelectByEmail(string email)
    {
        ThanhVien tv = new ThanhVien();
        Dictionary<String, Object> paralist = new Dictionary<String, Object>();
        paralist.Add("@Email", email);
        DataTable dt = this.DTExecuteSelect("ThanhVien_SelectByEmail", paralist);
        if (dt.Rows.Count != 0)
        {
            tv = ConvertObjectDataRowToThanhVien(dt.Rows[0]);
            return tv;
        }
        return null;


    }
    
    public string ChangePassword(string email, string oldpass, string newpass)
    {
        string result;
        ThanhVien tv = new ThanhVien();
        tv = this.SelectByEmail(email);
        if (tv != null &&  tv.Email == email)
        {
            if (tv.MatKhau == oldpass)
            {
                Dictionary<String, Object> paralist = new Dictionary<String, Object>();
                paralist.Add("@email", email);
                paralist.Add("@newpass", newpass);
                if (this.ExecuteUpdate("ThanhVien_ChangePass", paralist))
                {
                    result = "Thay doi mat khau thanh cong";
                    Mail mail = new Mail();
                    string body = "Mật khẩu đã được thay dổi thành công, mật khẩu mới của bạn là " + newpass + "";
                    result = result + mail.Send_Email(tv.Email, "Thay doi mat khau", body);
                }
                else
                {
                    result = "Thay đổi mật khẩu thất bại";
                }
            }
            else
            {
                result = "Sai mật khẩu!";
            }

        }
        else {
            result = "Không tồn tại tài khoản này!";
        }
        return result;
    }



    public string ChangeInfoMember(ThanhVien tv)
    {
        Dictionary<String, Object> paralist = new Dictionary<String, Object>();
        paralist.Add("@email", tv.Email);
        paralist.Add("@name", tv.HoTen);
        paralist.Add("@dayofbirth", tv.NgSinh);
        paralist.Add("@address", tv.DiaChi);
        paralist.Add("@phone", tv.Sodt);
        paralist.Add("@maquyen", tv.MaQuyen.MaQuyen);
        string result;
        if (this.ExecuteUpdate("ThanhVien_ChangeInfo", paralist))
        {
            result = "Thông tin của bạn đã được thay đổi";
        }
        else
        {
            result = "Thay đổi thông tin thất bại vui lòng xem lại các thông tin";
        }
        return result;

    }

    public string Insert(ThanhVien tv)
    {
        string result;
        if (this.CheckRegister(tv.Email))
        {
            Dictionary<String, Object> paralist = new Dictionary<String, Object>();
            paralist.Add("@Email", tv.Email);
            paralist.Add("@MatKhau", tv.MatKhau);
            paralist.Add("@HoTen", tv.HoTen);
            paralist.Add("@NgaySinh", tv.NgSinh);
            paralist.Add("@DiaChi", tv.DiaChi);
            paralist.Add("@Sodt", tv.Sodt);
            paralist.Add("@Quyen", tv.MaQuyen.MaQuyen);
            if (this.ExecuteUpdate("ThanhVien_Insert", paralist))
            {
                result = "Success";
            }
            else
            {
                result = "Fail";
            }
        }
        else
        {
            result = "MissedEmail";
        }
        return result;

    }

        public DataTable getEmail()
        {
            Dictionary<String, Object> paramlist = new Dictionary<String, Object>();
            return this.DTExecuteSelect("selectemail", paramlist);
        }


        public bool DeleteEmail(string email)
        {
            Dictionary<String, Object> paralist = new Dictionary<String, Object>();
            paralist.Add("@Email", email);
            bool success = this.ExecuteUpdate("deleteemail", paralist);
            return success;
        }

    public ThanhVien ConvertObjectDataRowToThanhVien(System.Data.DataRow dr)
    {
        ThanhVien tvien = new ThanhVien();
        tvien.Email = dr["EMAIL"].ToString();
        tvien.MatKhau = dr["MATKHAU"].ToString();
        tvien.HoTen = dr["HOTEN"].ToString();
        tvien.NgSinh = DateTime.Parse(dr["NGSINH"].ToString());
        tvien.DiaChi = dr["DIACHI"].ToString();
        tvien.Sodt = dr["SODT"].ToString();

        int roleId = int.Parse(dr["MAQUYEN"].ToString());
        IQuyen iRole = new DALQuyen();
        //Set property IdRole of Users
         tvien.MaQuyen= iRole.GetQuyenById(roleId);

        return tvien;
    }



    public bool updateQuyen(string email, int quyen)
    {
        Dictionary<String, Object> paralist = new Dictionary<String, Object>();
        paralist.Add("EMAIL", email);
        paralist.Add("MAQUYEN",quyen);
        return this.ExecuteUpdate("updateQuyen",paralist);
       

    }
}